<template>
  <div>


	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="关键字查询"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="getQueryShops">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
<!--
id
name
tel
registerTime
state1=待审核 2=审核通过待激活 3=激活成功 4=驳回
address
logo

-->
		<!--列表-->
		<el-table :data="this.pageInfo.dataList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="商铺名称"  >
			</el-table-column>
      <el-table-column prop="admin.username" label="管理员"  >
      </el-table-column>
			<el-table-column prop="tel" label="商铺电话"  >
			</el-table-column>

      <el-table-column prop="registerTime" label="创注册时间" >
      </el-table-column>
      <el-table-column prop="address" label="地址"  >
      </el-table-column>
			<el-table-column prop="logo" label="logo" >
        <template slot-scope="scope">
          <img :src="scope.row.logo" width="50px" height="50px"/>
        </template>
			</el-table-column>
			<el-table-column prop="state" label="状态" >
        <template slot-scope="scope">
          <span v-if="scope.row.state==1" style="color: #dea726">待审核</span>
          <span v-if="scope.row.state==2" style="color: cyan">待激活</span>
          <span v-if="scope.row.state==3" style="color: green">激活成功</span>
          <span v-if="scope.row.state==4" style="color: red">驳回</span>


        </template>
			</el-table-column>

			<el-table-column label="操作" width="220px">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button size="small" @click="handleExamine(scope.$index, scope.row)" v-if="scope.row.state==1" >审核</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
		<!--	<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="this.query.pageSize" :total="this.pageInfo.total" style="float:right;">
			</el-pagination>-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="this.query.currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          style="float:right;"
          :total="this.pageInfo.total">
      </el-pagination>
		</el-col>

		<!--新增/添加界面-->
		<el-dialog title="新增" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveFrom" label-width="80px" :rules="saveFromRules" ref="saveFrom">
				<el-form-item label="部门名称" prop="name">
					<el-input v-model="saveFrom.name" auto-complete="off"></el-input>
				</el-form-item>

        <el-form-item label="座机电话" prop="tel">
          <el-input v-model="saveFrom.tel" auto-complete="off"></el-input>
        </el-form-item>

<!--        <el-form-item label="地址" prop="address">-->
<!--          <el-input v-model="saveFrom.address" auto-complete="off"></el-input>-->
<!--        </el-form-item>-->

        <el-form-item prop="address" label="店铺地址">
          <el-input type="text" v-model="saveFrom.address" auto-complete="off" placeholder="请输入地址！"></el-input>
          <el-button size="small" type="primary" @click="selectAdrress">选择</el-button>
        </el-form-item>

        <el-table-column prop="logo" label="logo" >
          <template>
            店铺LOGO：<img :src="saveFrom.logo" width="80px" height="80px"/>
          </template>
        </el-table-column>

        <el-form-item prop="logo" label="修改Logo">
          <!--        <el-input type="text" v-model="shop.logo" auto-complete="off" placeholder="请输入logo！"></el-input>-->
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/common/fastDfs/"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              :file-list="fileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>


        <el-form-item label="管理员" prop="admin">
          <el-select v-model="saveFrom.admin" clearable value-key="id" placeholder="请选择">
            <!--下拉选项
            :key=""  //唯一标识
            :label 选择之后展示到选择框中的值
            :value 选中之后绑定给模型层的值  如果要绑定对象给模型层 有一个大坑
            必须要写  value-key="id"
            -->
            <el-option
                v-for="item in employees"
                :key="item.id"
                :label="item.username"
                :value="item">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.email }}</span>
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="状态">
          <el-radio-group v-model="saveFrom.state">
            <el-radio class="radio" :label="1">待审核</el-radio>
            <el-radio class="radio" :label="2">审核通过待激活</el-radio>
            <el-radio class="radio" :label="3">激活成功</el-radio>
            <el-radio class="radio" :label="4">驳回</el-radio>
          </el-radio-group>
        </el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

    <!--审核界面-->
    <el-dialog title="新增" :visible.sync="handleExamineVisible" :close-on-click-modal="false">
      <el-form :model="examine" label-width="80px" :rules="saveFromRules" ref="saveFrom">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="examine.name" disabled auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="座机电话" prop="tel">
          <el-input v-model="examine.tel" disabled auto-complete="off"></el-input>
        </el-form-item>


        <el-form-item prop="address"  label="店铺地址">
          <el-input type="text" disabled v-model="examine.address" auto-complete="off" placeholder="请输入地址！"></el-input>
<!--          <el-button size="small" type="primary" @click="selectAdrress">选择</el-button>-->
        </el-form-item>

        <el-table-column prop="logo" label="logo" >
          <template>
            店铺LOGO：<img :src="examine.logo" width="80px" height="80px"/>
          </template>
        </el-table-column>

        <el-form-item label="管理员"  prop="admin">
          <el-select v-model="examine.admin" disabled clearable value-key="id" placeholder="请选择">
            <!--下拉选项
            :key=""  //唯一标识
            :label 选择之后展示到选择框中的值
            :value 选中之后绑定给模型层的值  如果要绑定对象给模型层 有一个大坑
            必须要写  value-key="id"
            -->
            <el-option
                v-for="item in employees"
                :key="item.id"
                :label="item.username"
                :value="item">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.email }}</span>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态">
          <el-radio-group v-model="examine.state">
            <el-radio class="radio" :label="1">待审核</el-radio>
            <el-radio class="radio" :label="2">审核通过待激活</el-radio>
<!--            <el-radio class="radio" :label="3">激活成功</el-radio>-->
            <el-radio class="radio" :label="4">驳回</el-radio>
          </el-radio-group>
        </el-form-item>

        <div v-if="examine.state==4">
          请输入驳回原因：
          <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="examine.textarea">
          </el-input>
        </div>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="handleExamineVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmitExamine" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>


	</section>
    <el-dialog
        title="选择地址"
        :visible.sync="dialogVisable"
        width="30%">
      <baidu-map :center="{lng: 116.403765, lat: 39.914850}" @click="getLocal" :zoom="11">
        <bm-view class="map"></bm-view>
        <bm-control :offset="{width: '10px', height: '10px'}">
          <!--:sugStyle="{zIndex: 2100} 让搜索提示上浮-->
          <bm-auto-complete v-model="keyword" :sugStyle="{zIndex: 2100}">
            <div style="margin-bottom:10px">
              <input id="searchInput" type="text" placeholder="请输入关键字" class="searchinput"/>
              <el-button type="success" @click="selectAdrressConfirm">确定</el-button>
            </div>
          </bm-auto-complete>
        </bm-control>
        <bm-local-search :keyword="keyword" :auto-viewport="true" ></bm-local-search>
      </baidu-map>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisable=false">取 消</el-button>
        <el-button type="primary" @click="dialogVisable=false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'


	export default {
		data() {
			return {

			  keyword:'',
			  dialogVisable:false,
        fileList: [],
        query: {
					keyword: '',
          currentPage:1,
          pageSize:10
				},
        pageInfo:{
          dataList: [],
          total: 0
        },
				page: 1,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
        handleExamineVisible: false,
				saveFormVisible: false,//新增界面是否显示
				addLoading: false,
				saveFromRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//新增界面数据
				saveFrom: {
          id:null,
					name: "",
          tel:"",
          address:"",
          logo:"",
          lon:"",
          lat:"",
          state:"",
          admin:{
            username:"",
            phone:"",
            email:"",
            password:"",
            confirmPassword:""
          }
				},
        examine: {
          id:null,
          name: "",
          tel:"",
          address:"",
          logo:"",
          lon:"",
          lat:"",
          state:"",
          admin:{
            username:"",
          },
          textarea:""
        },

        employees:[],
        //
        deptTree:[]

			}
		},
		methods: {
      getLocal(e){
        console.log(e)
        this.saveFrom.lon=e.point.lng
        this.saveFrom.lat=e.point.lat
      },

      selectAdrressConfirm(){
        //获取值搜索框值,设置给地址
        var searchInputV=document.getElementById("searchInput").value;
        this.shop.address = searchInputV;
        //关闭对话框
        this.dialogVisable = false;
      },
      selectAdrress(){
        this.dialogVisable = true;
      },

      //图片预览
      handlePreview(file) {
        console.log(file);
      },
      //文件删除
      handleRemove(file, fileList) {
        var filePath =file.response.resultObj;
        this.$http.delete("/common/fastDfs/?path="+filePath)
            .then(res=>{
              if(res.data.success){
                this.$message({
                  message: '删除成功!',
                  type: 'success'
                });
                this.shop.logo="";
              }else{
                this.$message({
                  message: '删除失败!',
                  type: 'error'
                });
              }
            })
      },
      //文件上传成功回调
      handleSuccess(response, file, fileList){
        this.shop.logo = response.resultObj;
      },

		  //查询管理员 数据
      getEmployees(){
        // 发请求
        this.$http.get("/employee")
            .then(result=>{
              result = result.data
              if (result.success){
                this.employees=result.resultObj
              }else{
                this.$message({message: '员工查询失败', type: 'danger' });
              }
            })
            .catch(result=>{
            })
      },

		  //--------------------------------------------------------------
      handleSizeChange(val){
        this.query.pageSize = val;
        this.getShops()
      },
		  // 点击查询按钮
      getQueryShops(){
        this.query.currentPage = 1
        this.getShops()
      },
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			handleCurrentChange(val) {
				this.query.currentPage = val;
        this.getShops()
			},

			//获取XX列表
			getShops() {
        // No 'Access-Control-Allow-Origin'
        // 只要看到浏览器报这个错误就是跨域问题 前后端分离的项目一定会有跨越问题
				this.listLoading = true;
        // 发请求
        // localhost:8080/shop  全局的前缀
        this.$http.put("/shop",this.query)
            .then(result=>{
              result = result.data
              if (result.success){
                this.$message({message: '查询成功', type: 'success' });
                this.listLoading = false;
                this.pageInfo = result.resultObj
                console.log(this.pageInfo.dataList);
              }else{
                this.$message({message: '查询失败', type: 'danger' });
              }
            })
            .catch(result=>{

            })

			},

			//删除

			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true; // 模态框
					//NProgress.start();
					let para = { id: row.id };
					//发请求
          this.$http.delete("/shop/"+row.id)
          .then(result=>{
            result = result.data
            if (result.success){
              this.$message({message: '删除成功', type: 'success' });
              this.listLoading = false;
              this.query.currentPage = 1;
              // 刷新列表
              this.getShops()
            }else{
              this.$message({message: '删除失败', type: 'danger' });
            }
          })
				}).catch(() => {

				});
			},

			//显示编辑界面
			handleEdit: function (index, row) {
				this.saveFormVisible = true;
        this.saveFrom= Object.assign({}, row);  //赋值

        this.saveFrom.state=parseInt(row.state) //处理状态回显 ,
        this.getEmployees()

        //处理级联下拉回显
        let path = row.path;
       var splist= path.split("/")
        var p=[];
        console.log(splist)
        for (let i = 1; i < splist.length-1; i++) {
            p.push(Number.parseInt(splist[i]))
        }
        console.log(p)
        this.saveFrom.parent=p
      },

      //显示审核界面
      handleExamine: function (index, row) {
        this.handleExamineVisible = true;
        this.examine= Object.assign({}, row);  //赋值

        this.examine.state=parseInt(row.state) //处理状态回显 ,
        this.getEmployees()
        //处理级联下拉回显
        let path = row.path;
        var splist= path.split("/")
        var p=[];
        console.log(splist)
        for (let i = 1; i < splist.length-1; i++) {
          p.push(Number.parseInt(splist[i]))
        }
        console.log(p)
        this.examine.admin=p
      },

			//显示新增界面
			handleAdd: function () {
				this.saveFormVisible = true;
				this.saveFrom = {
          id:null,
          name: "",
          intro:"",
          manager:{
            id:null,
            username:""
          },
          parent:{
            id:null,
            name:""
          },
          state:""
        };
				this.getEmployees()

			},
			//编辑

      ///-----------------------------------------------------
			// 提交审核
      addSubmitExamine: function () {
				this.$refs.saveFrom.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;

							let para = Object.assign({}, this.examine);
              console.log(para);

              // 发请求
              this.$http.post("/shop/examine",para)
                  .then(result=>{
                    result = result.data
                    if (result.success){
                      this.$message({message: '保存成功', type: 'success' });
                      this.addLoading = false;
                      this.handleExamineVisible=false; //成功关闭窗口
                      this.query.currentPage = 1;
                      // 刷新列表
                      this.getShops()
                    }else{
                      this.$message({message: '保存失败', type: 'danger' });
                    }
                  })
                  .catch(result=>{
                    this.$message({message: '网络故障', type: 'danger' });
                  })
            })
                .catch(() => {
                  this.$message({message: '网络故障', type: 'danger' });
                });

					}
				});
			},


      //新增  / 修改
      addSubmit: function () {
        this.$refs.saveFrom.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.addLoading = true;

              let para = Object.assign({}, this.saveFrom);
              console.log(para);

              // 发请求
              this.$http.post("/shop",para)
                  .then(result=>{
                    result = result.data
                    if (result.success){
                      this.$message({message: '保存成功', type: 'success' });
                      this.addLoading = false;
                      this.saveFormVisible=false; //成功关闭窗口
                      this.query.currentPage = 1;
                      // 刷新列表
                      this.getShops()
                    }else{
                      this.$message({message: '保存失败', type: 'danger' });
                    }
                  })
                  .catch(result=>{
                    this.$message({message: '网络故障', type: 'danger' });
                  })
            })
                .catch(() => {
                  this.$message({message: '网络故障', type: 'danger' });
                });

          }
        });
      },

			selsChange: function (sels) {
				this.sels = sels;
			},

			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					// 发请求
          this.$http.patch("/shop",ids)
              .then(result=>{
                result = result.data
                if (result.success){
                  this.$message({message: '批量删除成功', type: 'success' });
                  this.listLoading = false;
                  this.query.currentPage = 1;
                  // 刷新列表
                  this.getShops()
                }else{
                  this.$message({message: '批量删除失败', type: 'danger' });
                }
              })
              .catch(result=>{

              })
          })
          .catch(() => {

          });
			}
		},
		mounted() {
			this.getShops();
		}
	}

</script>

<style lang="scss" scoped>
.map {
  width: 100%;
  height: 300px;
}
.searchinput{
  width: 300px;
  box-sizing: border-box;
  padding: 9px;
  border: 1px solid #dddee1;
  line-height: 20px;
  font-size: 16px;
  height: 38px;
  color: #333;
  position: relative;
  border-radius: 4px;
}
</style>